<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="icon" href="../../logo.svg" />
    <title>自定义指令</title>
  </head>
  <body>
    <div id="root">
      <div>当前值：<span v-text="n"></span></div>
      <div>当前值放大10倍：<span v-big-num="n"></span></div>
      <div>
        <button @click="n++">值+1</button>
      </div>
      <hr />
      <div>
        <input type="text" v-focus-bind="n" />
      </div>
    </div>

    <script src="../../vue.js"></script>
    <script>
      Vue.config.productionTip = false; // 阻止 vue 在启动时生成生产提示。

      new Vue({
        el: "#root",
        data: {
          n: 1,
        },
        directives: {
          "big-num"(element, binding) {
            const { value } = binding;

            element.innerText = value * 10;
          },
          "focus-bind": {
            // 指令与元素成功绑定时（一上来）
            bind(element, { value }) {
              element.value = value;
              console.log("bind");
            },
            // 指令所在元素被插入页面时
            inserted(element, { value }) {
              element.focus();
              console.log("inserted");
            },
            // 指令所在模板被重新解析时
            update(element, { value }) {
              element.value = value;
              console.log("update");
            },
          },
        },
      });
    </script>
  </body>
</html>
